package com.ta.gulimall.coupon.config.rabbitmq;//package com.ta.gulimall.order.config.rabbitmq;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
//import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.annotation.PostConstruct;
//
//@Configuration
//@Slf4j
//public class RabbitmqConfig {
//    @Autowired
//    RabbitTemplate rabbitTemplate;
//
//    @PostConstruct
//    private void init(){
//        /**
//         * 成功或者失败都有回调
//         * @param correlationData 消息内容
//         * @param ack 是否成功
//         * @param cause 失败的原因
//         */
//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            if(ack){
//                // 成功
//                log.info("交换机已经收到了消息Id为 : {}");
//            }else {
//                // 失败
//                log.info("交换机未接收到消息 : {},原因为 : {}",cause);
//            }
//        });
//
//        /**
//         * 消息退回，指消息未达到目的地被回退
//         * @param message 退回的消息
//         * @param replyCode 错误码
//         * @param replyText 错误原因
//         * @param exchange 交换机
//         * @param routingKey 路由key
//         */
//        rabbitTemplate.setReturnCallback((message,replyCode,replyText,exchange,routingKey)->{
//            System.out.println();
//            log.info("消息:{},被交换机:{}退回,退回原因:{},退回routingKey:{}",new String(message.getBody()),exchange,replyText,routingKey);
//        });
//    }
//
//        @Value("${spring.rabbitmq.host}")
//    private String host;
//
//    @Value("${spring.rabbitmq.port}")
//    private int port;
//
//    @Value("${spring.rabbitmq.virtual-host}")
//    private String virtualhost;
//
//    @Bean
//    public ConnectionFactory connectionFactory() {
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
//        connectionFactory.setVirtualHost(virtualhost);
//        //connectionFactory.setPublisherConfirms(true);
//        return connectionFactory;
//    }
//
//    @Bean
//    //必须是prototype类型
//    public RabbitTemplate rabbitTemplate() {
//        RabbitTemplate template = new RabbitTemplate(connectionFactory());
//        return template;
//    }
//
//    @Bean
//    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        return factory;
//    }
//}